//
//  main.swift
//  C150729_sqlite
//
//  Created by yao_yu on 15/7/29.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import Foundation

print("Hello, World!")

let db_path = "/Volumes/Data/Document/Test/filmoteca.sqlite".cStringUsingEncoding(NSUTF8StringEncoding)
print(db_path)

var db:COpaquePointer = nil
var statement:COpaquePointer = nil
let status = sqlite3_open(db_path!, &db)
if(status == SQLITE_OK){
    print("打开数据库成功")
}
else{
    print("打开数据库失败")
}

let sql = "select * from pelicula"
if (sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK){
    print("选择成功")
    var isRow = true
    if isRow {
        let columnCount = sqlite3_column_count(statement)
        for index in 0..<columnCount {
            // Get column name
            let name = sqlite3_column_name(statement, index)
            print(String.fromCString(name)!)
            // Get column type
            
        }
    }
    while(sqlite3_step(statement) == SQLITE_ROW) {
//        let dict = 
        let director = sqlite3_column_text(statement, 1)
        let buf_director = String.fromCString(UnsafePointer<CChar>(director))
        let imagen = sqlite3_column_text(statement, 2)
        let buf_imagen = String.fromCString(UnsafePointer<CChar>(imagen))
        let titulo = sqlite3_column_text(statement, 3)
        let buf_titulo = String.fromCString(UnsafePointer<CChar>(titulo))
        let year = sqlite3_column_text(statement, 4)
        let buf_year = String.fromCString(UnsafePointer<CChar>(year))
        
        print( ["director": buf_director!, "imagen": buf_imagen!, "titulo": buf_titulo!, "year": buf_year!])
    }
    sqlite3_finalize(statement)
}

sqlite3_close(db)
